define([
    'jquery',
    'error_code',
    'net',
    'data_const',
    'text!views/login.html'
], function($, ErrorCode, Net, DC, html) {
    'use strict';
    var net = Net.getInstance();
    function f(){
        
    }
    f.run = function(cb){
        var wrapper = $("#wrapper");
        wrapper.html("");
        wrapper.append($(html));
        var obj;
        try{
            obj = JSON.parse(window.localStorage.getItem("login_data"));
        }catch(e){
            obj = undefined;
            console.log("local login data error")
        }
        if(obj){
            $("#userTi").val(obj.user);
            $("#pwdTi").val(obj.pwd);
        }
        var loginBtn = $("#loginBtn");
        loginBtn.off("touchstart click");
        loginBtn.on("touchstart click", function(event){
            var user = $("#userTi").val(); 
            var pwd = $("#pwdTi").val();
            if(user === "" || pwd === ""){
                return;
            }
            net.post(DC.URL_LOGIN, {user: user, "pwd": pwd}, function(res){
                if(res.code === 0){
                    obj = {
                        user: user,
                        pwd: pwd
                    };
                    window.localStorage.setItem("login_data", JSON.stringify(obj));
                    if(typeof cb === "function"){
                        cb(); 
                    }
                }else if(res.code === ErrorCode.CODE_USER_PWD_ERROR){
                    console.log("error user pwd");
                }
            })
        });
    }
    
    return f; 
});